package com.sk.cardeal.act.home.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.CostType;
import com.sk.cardeal.entity.DataDictionary;
import com.sk.cardeal.entity.DataDictionaryList;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.SnackUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.listeners.OnSingleWheelListener;
import cn.addapp.pickers.picker.SinglePicker;
import cn.addapp.pickers.widget.WheelView;

/**
 * Created by sk on 2017/12/27.
 */

public class NewCostActivity extends BaseActivity implements HandleDataCallBack {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.cost_type)
    TextView costType;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.remark)
    TextView remark;
    @BindView(R.id.save)
    TextView save;
    @BindView(R.id.cost_name)
    TextView costName;
    @BindView(R.id.price)
    EditText price;
    @BindView(R.id.cost_price)
    EditText costPrice;
    @BindView(R.id.ed_remark)
    EditText edRemark;

    private LoadingDialog loadingDialog;
    private List<DataDictionary> allDataList = new ArrayList<>();

    @Override
    protected int getContentView() {
        return R.layout.activity_new_cost;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        loadingDialog.show();
        AppApi.getAllDataDictionary("FYLX", NewCostActivity.this, 0, NewCostActivity.this);
    }

    @OnClick({R.id.back, R.id.save, R.id.cost_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save:

                if(TextUtils.isEmpty(costName.getText().toString())){
                    SnackUtils.showError(mRootView,"请选择费用类型！");
                    return;
                }

                if(TextUtils.isEmpty(costPrice.getText().toString())){
                    SnackUtils.showError(mRootView,"请输入金额！");
                    return;
                }

                if(TextUtils.isEmpty(costPrice.getText().toString())){
                    SnackUtils.showError(mRootView,"请输入成本！");
                    return;
                }

                CostType costType = new CostType();
                costType.setCostName(costName.getText().toString());
                costType.setCost(costPrice.getText().toString());
                costType.setPrice(price.getText().toString());
                costType.setRemark(edRemark.getText().toString());
                Intent intent = new Intent();
                intent.putExtra("data", costType);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.cost_name:
                List<String> stringList = new ArrayList<>();
                for(int i = 0 ;i < allDataList.size() ;i++){
                    stringList.add(allDataList.get(i).getName());
                }
                SinglePicker<String> picker = new SinglePicker<>(this, stringList);
                picker.setCanLoop(false);//不禁用循环
                picker.setLineVisible(true);
                picker.setShadowVisible(true);
                picker.setTextSize(30);
                picker.setSelectedIndex(0);
                picker.setWheelModeEnable(true);
                picker.setWeightEnable(true);
                picker.setWeightWidth(1);
                picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
                picker.setUnSelectedTextColor(0xFF999999);
                picker.setOnItemPickListener(new OnItemPickListener<String>() {
                    @Override
                    public void onItemPicked(int i, String s) {
                        costName.setText(s);
                    }
                });
                picker.show();
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        DataDictionaryList dataDictionaryList = JSON.parseObject(result, DataDictionaryList.class);
        allDataList.clear();
        allDataList.addAll(dataDictionaryList.getResultList());
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }
}
